package com.byxy.demo2.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.byxy.demo2.entity.Forum;
import com.byxy.demo2.entity.Reply;
import com.byxy.demo2.entity.Tab;
import com.byxy.demo2.entity.Tip;
import com.byxy.demo2.entity.User;
import com.byxy.demo2.service.ForumService;
import com.byxy.demo2.service.ReplyService;
import com.byxy.demo2.service.TabService;
import com.byxy.demo2.service.TipService;
import com.byxy.demo2.vo.TipVO;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
public class TipController {
	
	
	
	@Autowired
	private ForumService forumService;
	
	@Autowired
	private TabService tabservice;
	
	@Autowired
	private TipService tipservice;
	
	@Autowired
	private ReplyService replyservice;
	
	@GetMapping("toPublishTipPage.do")
	public String add(Model model){
		List<Forum> forum=forumService.findAll();
		model.addAttribute("forums", forum);
		List<Tab> tab=tabservice.findAll();
		model.addAttribute("tabs", tab);
		return "publishTip";
	}
	
	@PostMapping("publishNewTip.do")
	
	public String doAdd(Tip tip, String selectedForumId,HttpSession session) {
		log.info("TipContent = "+ tip.getTipContent());
		log.info("tipTitle = "+ tip.getTipTitle());
		log.info("TabId = "+ tip.getTabId());
		log.info("selectedForumId = "+ selectedForumId );
		User u = (User)session.getAttribute("USER");
		tip.setUserId(u.getUserId());
		tip.setTipClick(0);
		tip.setTipIsdeleted(0);
		tip.setTipIsKnot(0);
		tip.setTipModifyTime(new Date());
		tip.setTipPublishTime(new Date());
		tip.setTipReplies(0);
		tipservice.add(tip);
		return "redirect:toMainPage.do";
	}
	
	@GetMapping("showTip.do")
	public String showTip(Integer tipId,Model model ){
		log.info("tipId = " + tipId);
		TipVO tipVo = tipservice.findById(tipId);
		model.addAttribute("tip", tipVo);
		List<Reply> replies = replyservice.findByTip(tipId);
		model.addAttribute("replies", replies);
		return "tipContent";
		
	}
	
}
